package main

import (
	"fmt"
	"os"
	"strconv"

	"github.com/ethereum/go-ethereum/common/hexutil"
	"github.com/ethereum/go-ethereum/rpc"
)

// parameters here
var (
	// rpc address and port
	ethrpc = ""
	startB = 1
	endB   = 800
)

func main() {
	if len(os.Args) < 4 {
		fmt.Println("at least 3 args: <rpc> <start> <end>")
		return
	}
	ethrpc = os.Args[1]
	var err error
	startB, err = strconv.Atoi(os.Args[2])
	if err != nil {
		fmt.Printf("start wrong: %s\n", err)
		return
	}
	endB, err = strconv.Atoi(os.Args[3])
	if err != nil {
		fmt.Printf("end wrong: %s\n", err)
		return
	}
	// connect the client
	client, err := rpc.Dial(ethrpc)
	if err != nil {
		fmt.Printf("No client listens at %s\n", ethrpc)
		os.Exit(1)
	}

	fmt.Printf("Start from: %d, end to: %d\n", startB, endB)
	for i := startB; i <= endB; i++ {
		res := make(map[string]interface{})
		if err = client.Call(&res, "eth_getBlockByNumber", hexutil.EncodeUint64(uint64(i)), false); err != nil {
			panic(err)
		}
		curSize, _ := hexutil.DecodeUint64(res["size"].(string))
		fmt.Printf("%d %d\n", curSize, len(res["transactions"].([]interface{})))
	}
	fmt.Println("Extracting blocks finish")
}
